/**
 * Created by WH on 2017/11/22.
 */
$(function () {
    var jumpNumber = GetQueryStringLan('questionNumber')? GetQueryStringLan('questionNumber') : 2 ;
    $('#type-title').text($('.swiper-wrapper dl:first').attr('type-title'));
    $("#totnum").text($('.select-num').length);
    var intDiff = parseInt(6000);//计时总分钟
    //window.sessionStorage.setItem('intDiff', intDiff);
    var minute = 0,
        hourse = 0,
        second = 0; //时间默认值
    var questionAnswer = '';

    /**
     * 正计时
     */
    AddTime();
    function AddTime() {
        var timeNumber = 0;
        window.setInterval(function () {
            timeNumber++;
            var minute =  Math.floor(timeNumber/60);
            var hourse = Math.floor(minute/60);
            var second = timeNumber - minute*60 - hourse * 60 * 60;
            $('#last-question-time').html(hourse+':'+minute + ':' + second);
        },1000);
    }

    /**
     * 倒计时
     */
    // timer();
    function timer() {
        window.setInterval(function () {
            if (intDiff < 0) {
                $(".maskwhite").show();
                $(".subjuan").hide();
                $(".subjuanYes").show();
                e.preventDefault();
            }
            else {
                hourse = Math.floor(intDiff / 3600);
                minute = Math.floor((intDiff - (hourse * 3600))/60);
                second = Math.floor(intDiff) -(hourse * 3600) - (minute * 60);
                intDiff--;
            }
            if (hourse <= 9) hourse = '0' + hourse;
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            $('#last-question-time').html(hourse + ":" + minute + ':' + second);
            if (intDiff / 60 > 0) {
                $("#subTime").text(Math.ceil(intDiff / 60));
            } else {
                $("#subTime").text(0);
            }
        }, 1000);
    }

    //题目轮播
    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        prevButton: '.swiper-button-prev',
        nextButton: '.swiper-button-next',
        paginationClickable: true,
        paginationBulletRender: function (index, className) {
            $("#totnum").text(index + 1);
            var html = '';
            html += '<span class="' + className + '">' + (index + 1) + '</span>';
            return html;//返回所有的页码html
        },
        onSlideChangeStart: function (swiper) {
            questionAnswer = '';
            // $("#curnum").text(swiper.activeIndex + 1);//当前页
            // var num  = $('.swiper-slide-active').attr('data-num')
            $('#type-title').text($('.swiper-slide-active').attr('type-title'));
            $("#curnum").text($('.swiper-slide-active').attr('data-num'));//当前页
            $("#totnum").text($('.swiper-slide-active').attr('data-total'));//一共几页
            var isCollection = $('.swiper-slide-active').attr('data-iscollection');
            if(isCollection=='1'){
                //0：为收藏 1：已收藏
                $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_yishoucang.png')
                $('.collectionTab').text('收藏')
            } else {
                //0：为收藏 1：已收藏
                $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_shoucang.png')
                $('.collectionTab').text('已收藏')
            }
        }
    });

    //刚进页面初始化收藏按钮样式
    installCollection();
    function installCollection() {
        var isCollection = $('.swiper-slide-active').attr('data-iscollection');
        if(isCollection=='1'){
            //0：为收藏 1：已收藏
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_yishoucang.png');
            $('.collectionTab').text('已收藏')
        } else {
            //0：为收藏 1：已收藏
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_shoucang.png')
            $('.collectionTab').text('收藏')
        }
    }


    //点击底部出现题目数card-close
    $("#numok").click(function () {
        $(".maskwhite").toggle();
        $(".card-top").toggle();
    });

    /*点击答题卡标题号*/
    $('.select-num').click(function () {
        questionAnswer = '';
        var slideIndex = parseInt($(this).text()) - 1;
        $("#curnum").text($(this).text());
        $('#type-title').text($(".swiper-wrapper dl").eq(slideIndex).attr('type-title'));
        swiper.slideTo(slideIndex, 0, false);
        $(".maskwhite").toggle();
        $(".card-top").toggle();
    });

    //收藏取消收藏
    $("#showall").click(function () {
        var isCollection = $('.swiper-slide-active').attr('data-iscollection');
        if(isCollection=='0'){
            //0：为收藏 1：已收藏
            $('.swiper-slide-active').attr('data-iscollection','1');
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_yishoucang.png');
            $('.collectionTab').text('已收藏')
        } else {
            //0：为收藏 1：已收藏
            $('.swiper-slide-active').attr('data-iscollection','0');
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_shoucang.png')
            $('.collectionTab').text('收藏')
        }
    });

    //点击查看答案以及解析
    /**
     * 如果是继续做题状态 ，则需要在html赋值的时候判断data-isDo 的状态是否已经做过
     * 0:未做过 1：表示做题中 2：查看解析中不能再次执行题目的点击事件
     */

    $(document).on('click','.see-analysis',function () {
        if($(this).attr('data-isDo')=="1"){
            $(this).parent().find('.exam-analysis-box').removeClass('hidden');
            $(this).addClass('hidden');
            $(this).attr('data-isDo','2');
        } else {
            showToast('请先答题');
        }
    });


    function showToast(str) {
        $('#toast-text').text(str);
        $('#toast').css('display','block');
        setTimeout(function () {
            $('#toast').css('display','none');
        },3000);
    }


    //单选择答案
    $("dl.option-one dd").click(function () {
        if($(this).parent('dl').find('.see-analysis').attr('data-isDo')=='2'){
            return;
        }
        $(this).parent('dl').find('.see-analysis').attr('data-isDo','2');
        $(this).parent('dl').find('.exam-analysis-box').removeClass('hidden');
        $(this).parent("dl").find("dd").removeClass("chance");
        $(this).parent("dl").find(".option-img").css({'border':'1px solid #666','color':'#666','background-color':'transparent'});
        $(this).addClass("chance");
        $(this).find('.option-img').css({'border':'1px solid #ffa930','color':'#fff','background-color':'#ffa930'});
        var questionAnswer = $(this).find('.option-select').text();
        var indexnum = $(this).parent("dl").index();
        $(".select-num").eq(indexnum).addClass("choose-option");
        $(".select-num").eq(indexnum).attr('question-answer', questionAnswer)
    });

    //多选择答案
    $("dl.option-two dd").click(function () {

        if($(this).parent('dl').find('.see-analysis').attr('data-isDo')=='2'){
            return;
        }

        if ($(this).hasClass('chance')) {
            var doNumber = $(this).parent().find('.chance').length;
            if(doNumber == 1){
                return;
            }
            $(this).removeClass("chance");
            $(this).find('.option-img').css({'border':'1px solid #666','color':'#666','background-color':'transparent'});
        } else {
            $(this).addClass("chance");
            $(this).find('.option-img').css({'border':'1px solid #ffa930','color':'#fff','background-color':'#ffa930'});
        }
        if ($(this).parent("dl").find("dd").hasClass("chance")) {
            var indexnum = $(this).parent("dl").index();
            $(".select-num").eq(indexnum).addClass("choose-option");
        } else {
            var indexnum = $(this).parent("dl").index();
            $(".select-num").eq(indexnum).removeClass("choose-option");
        }
        $(this).parent('dl').find('.see-analysis').attr('data-isDo','1');
        var doubleNum = $(this).parent().find('.chance');
        questionAnswer = '';
        for (var i = 0; i < doubleNum.length; i++) {
            questionAnswer += doubleNum.eq(i).find('.option-select').text();
        }
        $(".select-num").eq(indexnum).attr('question-answer', questionAnswer)
    });

    /*获取答题卡选项*/
    function subCard() {
        questionDetail.QuestionList = [];
        var selectNum = $('.select-num.choose-option');
        for (var i = 0; i < selectNum.length; i++) {
            var optionDetail = {};
            var QuestionId = selectNum.eq(i).attr('question-id');
            var QuestionType = selectNum.eq(i).attr('question-type');
            var userAnswer = selectNum.eq(i).attr('question-answer');
            optionDetail.QuestionId = QuestionId;
            optionDetail.QuestionType = QuestionType;
            optionDetail.UserAnswer = userAnswer;
            questionDetail.QuestionList.push(optionDetail);
        }
        console.log('答题记录', questionDetail)
    }


    $("#subno,.alert-close").click(function () {//取消
        $(".maskwhite").hide();
        $(".subjuan").hide();
        $('#numok .tab-label').css('color', '#333');
        $('#numok .navIcon').attr('src', '../../public/images/examView/bnt_jiaojuan_gray.png')
    });

    $("#subyes,#noTime").click(function () {
        $(".maskwhite").hide();
        $(".subjuan").hide();
        $(".subjuanYes").hide();
        subCard();
    });

    $('#backPage').click(function () {
        alert('小崔帅比');
    });

    $("#reSub").click(function () {
        window.location.href = "/exam/totle?PaperId=" + paperId + '&CourseId=' + courseId
    })

    $(document).on('click','.exam-question-img',function () {
        var str = $(this).attr('src');
        $('.alert-img-detail').attr('src',str);
        $('.alert-img').css('display','block');
    })

    $(document).on('click','.alert-img',function () {
        $('.alert-img').css('display','none');
    })

    //正确率扇形
    creatSector();
    function creatSector() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('sector1'));
        // 指定图表的配置项和数据
        var option = {
            tooltip : {
                show: false,
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            series: [
                {
                    name:'访问来源',
                    type:'pie',
                    radius: '100%',
                    avoidLabelOverlap: false,
                    hoverAnimation:false,
                    legendHoverLink:false,
                    color: ['#0098e1', '#f3f3f3'],
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:[
                        {value:335},
                        {value:310}
                    ]
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
});